// pages/tabBar/index/index.js
import tui from './../../../common/httpRequest'
import ZhToPinYin from '../../../components/common/tui-zh-pinyin/tui-zh-pinyin';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    url: ' https://image.anji-navinfo.cn/group1/M00/9F/9F/img/',
    money: ' https://image.anji-navinfo.cn/group1/M00/9F/9F/img/money.png',
    lucky: ' https://image.anji-navinfo.cn/group1/M00/9F/9F/img/lucky.png',
    flag: '/static/images/img/flag.png',
    logout: ' https://image.anji-navinfo.cn/group1/M00/9F/9F/img/logout.png',
    head: '/static/images/tx/no1.jpeg',
    myHead: ' https://image.anji-navinfo.cn/group1/M00/9F/9F/img/zhaoying.jpg',
    headOther: '',
    name: '姓名',
    phone: '8008208820',
    pinyin: '',
    zhToPinYin: null,
    department: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    getApp().globalData.ifHide = '2'
    getApp().globalData.searchName = ''
    this.data.zhToPinYin = new ZhToPinYin(false);
    // this.pinyinSpace = this.zhToPinYin.getPinYin(wx.getStorageSync("name"), true)
    let pinyin = this.data.zhToPinYin.getPinYin(wx.getStorageSync("name"), false)

    wx.setStorageSync('searchName', '');

    this.getHeight()
    let regx = /(1[3|4|5|7|8][\d]{9}|0[\d]{2,3}-[\d]{7,8}|400[-]?[\d]{3}[-]?[\d]{4})/g;
    this.setData({
      name: wx.getStorageSync("name"),
      phone: this.matchPhoneNum(wx.getStorageSync("account"),regx),
      pinyin: pinyin,
      department: wx.getStorageSync("department"),
      headOther: this.data.url + pinyin + '.png'
    })
  },
  matchPhoneNum(str,regx){
    
    // console.Log("替换前的文本内容:",str)
    // 查找出文本中的所有手机号码
    var phoneNums = str.match( regx)
    console.log( "phoneNums", phoneNums)
    if(phoneNums){
    for(var i= 0;i<phoneNums .length;i++){//手机号全部替换
    var temp = phoneNums[i]
    //隐藏手机号中间4位(例如:153012345678,隐藏后为 153****4567)
    temp = temp.replace(/^(\d{3})\d{4}(\d{4})$/,"$1****$2");
    str = str.replace(phoneNums[i] ,temp)
    // console.Log("替换后的文本内容:",str)
    return str;
    }
  }
  },
imgError(e) {
  this.setData({
    headOther: this.data.head
  })

},
toMoney() {
  wx.navigateTo({
    url: '/packages/money/money',
  })
},
toLottery() {
  wx.navigateTo({
    url: '/packages/lottery/lottery',
  })
},
toFlag() {
  wx.navigateTo({
    url: '/packages/flag/list/list',
  })
},
getHeight() {
  const capsuleInfo = wx.getMenuButtonBoundingClientRect();//胶囊信息
  const systemInfo = wx.getSystemInfoSync(); // 获取设备信息
  const statusHeight = systemInfo.statusBarHeight // 状态栏高度
  const clientHeight = systemInfo.windowHeight, clientWidth = systemInfo.windowWidth, rpxR = 750 / clientWidth;
  const calc = clientHeight * rpxR;
  this.setData({
    winHeight: calc,
    height: capsuleInfo.height,
    statusHeight: statusHeight,
    top: (capsuleInfo.top + statusHeight) / 2,
    topD: 45 + statusHeight,
    heightPage: clientHeight - statusHeight - 45 - 300,
    infoHeight: clientHeight - statusHeight - 45 - 189 - 90,
    clientWidth: clientWidth - 20,
    heightOver: clientHeight - statusHeight - 45 - 189 - 100
  })
  // console.log(capsuleInfo.top, statusHeight)
},
openActionSheet(e) {
  let tips = "退出登录会清除您的登录信息，确认退出吗？";
  let itemList = [{
    text: "退出登录",
    color: "#E3302D"
  }];
  let color = "#9a9a9a";
  let size = 26;
  let isCancel = true;
  let maskClosable = true
  setTimeout(() => {
    this.setData({
      showActionSheet: true,
      itemList: itemList,
      maskClosable: maskClosable,
      tips: tips,
      color: color,
      size: size,
      isCancel: isCancel
    })

  }, 0)
},
// 退出登录
async itemClick() {
  this.setData({
    showActionSheet: false
  })
  const res = await tui.request('sys/user/quit', 'POST', {
    token: tui.getToken()
  }, false);
  if (res.code == 20000) {
    wx.navigateBack()
    wx.clearStorageSync()
    wx.reLaunch({
      url: '/pages/login/login'
    })
  }
},
 closeActionSheet() {
  this.setData({
    showActionSheet: false
  })

},
/**
 * 生命周期函数--监听页面初次渲染完成
 */
onReady() {

},

/**
 * 生命周期函数--监听页面显示
 */
onShow() {
  if (typeof this.getTabBar === 'function' &&
    this.getTabBar()) {
    this.getTabBar().setData({
      current: '3',
      ['tabBar[2].num']:getApp().globalData.ifExemine
    })
  }
},

/**
 * 生命周期函数--监听页面隐藏
 */
onHide() {

},

/**
 * 生命周期函数--监听页面卸载
 */
onUnload() {

},

/**
 * 页面相关事件处理函数--监听用户下拉动作
 */
onPullDownRefresh() {

},

/**
 * 页面上拉触底事件的处理函数
 */
onReachBottom() {

},

/**
 * 用户点击右上角分享
 */
onShareAppMessage() {

}
})